package org.lucene;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class LuceneMyCodecTest {

	final static String indexPath = "g:/lucene/codec/Lucene41x";

	/**
	 * 
	 */
	public static void search() {
		IndexReader reader;
		String fieldName = "GUID";
		String q = "*:*";
		try {
			reader = DirectoryReader.open(FSDirectory.open(new File(indexPath)));
			
			IndexSearcher searcher = new IndexSearcher(reader);
			
			Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_41);

			QueryParser parser = new QueryParser(Version.LUCENE_41, fieldName, analyzer);
			Query query = parser.parse(q);
			searcher.search(query, null, 100);
			TopDocs results = searcher.search(query, 5 * 20);
			ScoreDoc[] hits = results.scoreDocs;
			int numTotalHits = results.totalHits;
			System.out.println(numTotalHits + " total matching documents");
			TopDocs search = searcher.search(query, numTotalHits);
			hits = search.scoreDocs;
			System.out.println(hits.length);
			Document doc = searcher.doc(hits[1].doc);
			String id = doc.get("GUID");
			System.out.println(id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void index() {


		Analyzer analyzer = new CJKAnalyzer(Version.LUCENE_41);
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_41, analyzer);
		config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
		Codec codec = new org.nlp.lucene.codecs.lucene41.Lucene41Codec();
		config.setCodec(codec); // 设置编码器
		IndexWriter writer = null;
		try {
			Directory luceneDir = FSDirectory.open(new File(indexPath));
			writer = new IndexWriter(luceneDir, config);
			List<Document> list = new ArrayList<Document>();
			Document doc = new Document();

			doc.add(new StringField("GUID", UUID.randomUUID().toString(), Field.Store.YES));
			doc.add(new StringField("TYPE", "A", Field.Store.YES));
			doc.add(new TextField(
					"Content",
					"北京时间1月14日04:00(西班牙当地时间13日21:00)，2012/13赛季西班牙足球甲级联赛第19轮一场焦点战在纳瓦拉国王球场展开争夺.",
					Field.Store.YES));
			list.add(doc);

			doc.add(new StringField("GUID", UUID.randomUUID().toString(), Field.Store.YES));
			doc.add(new StringField("TYPE", "A", Field.Store.YES));
			doc.add(new TextField("Content", "巴萨超皇马18分毁了西甲？媒体惊呼 克鲁伊夫看不下去.", Field.Store.YES));
			list.add(doc);

			doc.add(new StringField("GUID", UUID.randomUUID().toString(), Field.Store.YES));
			doc.add(new StringField("TYPE", "B", Field.Store.YES));
			doc.add(new TextField("Content", "what changes in lucene4.", Field.Store.YES));
			list.add(doc);

			writer.addDocuments(list);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		index();
		search();

	}
}